VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



VB.Net - Write And Read Data Using {Singleton Pattern Design }, If U Are OOP Developer, Read This P

by Gehan Fernando (47 Submissions)
Category: Databases/Data Access/DAO/ADO
Compatability: VB.NET
Difficulty: Unknown Difficulty
Originally Published: Wed 7th November 2007
Date Added: Mon 8th February 2021
Rating: (1 Votes)

VB.Net - Write And Read Data Using {Singleton Pattern Design }, If U Are OOP Developer, Read This Pattern And Get Idea { To Learn More Get

Rate VB.Net - Write And Read Data Using {Singleton Pattern Design }, If U Are OOP Developer, Read This P




Option Explicit On

Imports System.Data
Imports System.Data.SqlClient

Public Class ClassDT

    Private Shared sqlClassDT As ClassDT
    Private Shared sqlcon As SqlConnection
    Private Shared getproductlist As List(Of String)

    Public Shared ReadOnly Property GetProducts() As List(Of String)

        Get
            Return getproductlist
        End Get

    End Property

    Sub New()

        sqlcon = New SqlConnection("Data Source=UDEESHA;Initial Catalog=Northwind;Integrated Security=True")
        sqlcon.Open()

    End Sub

    Protected Overrides Sub Finalize()

        MyBase.Finalize()

    End Sub

    Public Shared Function GetSQLConnection() As ClassDT

        If sqlClassDT Is Nothing Then
            sqlClassDT = New ClassDT()
            If getproductlist Is Nothing Then
                getproductlist = New List(Of String)
                getproductlist.AddRange(GetPrdcts())
            End If
        End If
        Return sqlClassDT

    End Function

    Private Shared Function GetPrdcts() As List(Of String)

        GetPrdcts = New List(Of String)
        GetPrdcts.Clear()

        Dim com As New SqlCommand("SELECT ProductID,ProductName FROM Products ORDER BY ProductID", sqlcon)
        com.CommandType = CommandType.Text
        Dim comreader As SqlDataReader
        comreader = com.ExecuteReader()

        While comreader.Read()
            GetPrdcts.Add(comreader.GetValue(0).ToString.PadRight(10) & comreader.GetValue(1).ToString)
        End While

        comreader.Close()
        com.Cancel()
        com.Dispose()

        Return GetPrdcts

    End Function

End Class

' -----------------------------------------------------

'Create Form

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim MyData As SingletonDataAccess.ClassDT = SingletonDataAccess.ClassDT.GetSQLConnection()
        ListBox1.DataSource = ClassDT.GetProducts()

    End Sub



Download this snippet    Add to My Saved Code

VB.Net - Write And Read Data Using {Singleton Pattern Design }, If U Are OOP Developer, Read This P Comments

No comments have been posted about VB.Net - Write And Read Data Using {Singleton Pattern Design }, If U Are OOP Developer, Read This P. Why not be the first to post a comment about VB.Net - Write And Read Data Using {Singleton Pattern Design }, If U Are OOP Developer, Read This P.

Post your comment

Subject:
Message:
0/1000 characters